//
//  StatusPictureView.swift
//  weibo
//
//  Created by 上官风雨 on 15/11/14.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
private let PictureCellId = "PictureCellId"
private let PictureCellMargin: CGFloat = 5

class StatusPictureView: UICollectionView {
    
    var imageURLs : [NSURL]?{
        
        didSet{
            
            reloadData()
            
            let viewsize = calceViewSize()
            
            self.snp_updateConstraints { (make) -> Void in
                
                make.width.equalTo(viewsize.width)
                make.height.equalTo(viewsize.height)
                
            }
            
                }
        
    }
    
    func calceViewSize ()->CGSize{
        
        let column :CGFloat = 3
        let maxwidth = screenW - 2 * StatusCellMargin
        
        //单图的宽度
        
        let itemWidth = (maxwidth - 2 * PictureCellMargin) / column
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        let imagecount = imageURLs?.count ?? 0
        
        if imagecount == 0 {
            
            return CGSizeZero
        }
        
        if imagecount == 1 {
            
            let key = imageURLs?.last?.absoluteString
            
            let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(key)
            
            if image == nil {
                
                return CGSizeMake(120, 180)
            }
            let imageSize = image.size
            
            layout.itemSize = imageSize
            
            return imageSize
        }
        
        if imagecount == 4{
            let w = itemWidth * 2 + PictureCellMargin
            
            return CGSizeMake(w, w)
        }
        
        //多图
        let row = CGFloat((imagecount - 1) / 3 + 1)
        
        let w = maxwidth
        let h = row * itemWidth + (row - 1) * PictureCellMargin
        
        return CGSizeMake(w, h)
        
    }
    

    init(){
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = PictureCellMargin
        layout.minimumInteritemSpacing = PictureCellMargin
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        
        
        registerClass(PictureCell.self, forCellWithReuseIdentifier: PictureCellId)
        dataSource = self
        
        backgroundColor = UIColor.whiteColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension StatusPictureView:UICollectionViewDataSource {
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
      return imageURLs?.count ?? 0
   
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureCellId, forIndexPath: indexPath) as! PictureCell
        cell.imageUrl = imageURLs![indexPath.item]
        
        return cell
    }
    
    
}

class PictureCell :UICollectionViewCell {
    
    
    var imageUrl : NSURL?{
        
        didSet{
            
            imageView.sd_setImageWithURL(imageUrl, placeholderImage: nil)
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
       contentView.addSubview(imageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
    lazy var imageView :UIImageView = {
       
        let iv = UIImageView()
        
        iv.contentMode = .ScaleAspectFill
        iv.clipsToBounds = true
        
        return iv
        
    }()
    
}


















